package com.jokins.admin.config.sql;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 自动填充创建时间和更新时间
        this.strictInsertFill(metaObject, "createdTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updatedTime", Date.class, new Date());

        // 假设创建人ID和更新人ID可以通过某种方式获取，例如从当前登录用户中获取
        String currentUserId = getCurrentUserId(); // 需要实现此方法
        this.strictInsertFill(metaObject, "createdById", String.class, currentUserId);
        this.strictInsertFill(metaObject, "updatedById", String.class, currentUserId);

        // 自动生成ID，可以使用UUID或其他生成策略
        this.strictInsertFill(metaObject, "id", String.class, java.util.UUID.randomUUID().toString());
        //this.strictInsertFill(metaObject, "brand", String.class, "Like-Project-Admin");
        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 自动填充更新时间
        this.strictUpdateFill(metaObject, "updatedTime", Date.class, new Date());

        // 假设更新人ID可以通过某种方式获取，例如从当前登录用户中获取
        String currentUserId = getCurrentUserId(); // 需要实现此方法
        this.strictUpdateFill(metaObject, "updatedById", String.class, currentUserId);
    }

    private String getCurrentUserId() {
        // 实现获取当前用户ID的逻辑，例如从Spring Security中获取
        return "currentUserId"; // 示例返回值
    }
}
